package com.shanghaionstar.user.presenter;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.more.response.VehicleImgRes;
import com.shanghaionstar.user.FeedBackResult;
import com.shanghaionstar.user.iview.IChooseVehicleImgView;
import com.shanghaionstar.user.service.ChooseVehicleImgService;

import java.util.List;

import rx.functions.Action1;

/**
 * Created by Mengzhou Gu on 2016/8/30.
 */
public class ChooseVehicleImgPresenter extends BasePresenter<List<VehicleImgRes>> {
    private IChooseVehicleImgView chooseVehicleImgView;
    private ChooseVehicleImgService service;
    public ChooseVehicleImgPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<List<VehicleImgRes>> view) {
        super.setView(view);
        this.chooseVehicleImgView=(IChooseVehicleImgView)view;
        service=new ChooseVehicleImgService(chooseVehicleImgView.onActivity());
        setServiceProvider(service);
    }

    public void getData(String imageID){
        chooseVehicleImgView.saveLoading(true);
        subscription=service.getObserval(imageID).subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<FeedBackResult>() {
                    @Override
                    public void call(FeedBackResult feedBackResult) {
                        if (!CommonUtil.isNull(feedBackResult)){
                            chooseVehicleImgView.saveLoading(false);
                            chooseVehicleImgView.saveSuccess(feedBackResult);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        chooseVehicleImgView.saveLoading(false);
                        chooseVehicleImgView.saveFailure((Exception) throwable);
                    }
                });
    }
}
